package org.example.auth.service.impl;

import org.example.auth.dto.LoginDTO;
import org.example.auth.service.ILoginService;
import org.example.auth.vo.LoginVO;
import org.example.base.context.LoginContext;
import org.example.org.domain.Employee;
import org.example.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.UUID;

@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private IEmployeeService service;
    @Override
    public LoginVO login(LoginDTO loginDTO) {
        Employee employee = service.selectByName(loginDTO.getAccount());
        if(Objects.isNull(employee)){
            throw new RuntimeException("账号或密码错误");
        }
        if(!employee.getPassword().equals(loginDTO.getCheckPass())){
            throw new RuntimeException("账号或密码错误");
        }
        String token= UUID.randomUUID().toString();
        LoginVO loginVO = new LoginVO(token, employee);
        LoginContext.loginMap.put(token,employee);

        return loginVO;
    }
}
